package com.speech;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.ishumei.smantifraud.SmAntiFraud;
import com.speech.http.ApiService;
import com.speech.http.Http;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.commonsdk.UMConfigure;

import java.util.ArrayList;
import java.util.List;

public class MyApplication extends Application {

    public static List<Activity> lstActivies = new ArrayList<>();

    private static MyApplication instance;

    private ApiService apiService;

    public static MyApplication getInstance() {
//        if (instance == null)
//            instance = new MyApplication();
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        apiService = Http.getService();
        instance = this;
        Fresco.initialize(this);

        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null);

        if(!AppConfig.DEBUG) {
            CrashReport.initCrashReport(getApplicationContext(), "275f57d662", AppConfig.DEBUG);
        }

        //数美
        if(getCurProcessName(this).equals(this.getPackageName())){
            SmAntiFraud.SmOption option = new SmAntiFraud.SmOption();
            String DEBUG_ORG = "xxx";// organization代码 非AccessKey
            option.setOrganization(DEBUG_ORG);
            option.setChannel("xxx");// 渠道代码
//            SmAntiFraud.create(this,option);
        }

    }

    public ApiService getApiService() {
        return apiService;
    }

    private String getCurProcessName(Context context){
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for(ActivityManager.RunningAppProcessInfo appProcess:mActivityManager.getRunningAppProcesses()){
            if(appProcess.pid == pid){
                return appProcess.processName;
            }
        }
        return null;
    }

    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        if (!lstActivies.contains(activity))
            lstActivies.add(activity);
    }

    // 遍历所有Activity并finish
    public void destory() {

        for (Activity activity : lstActivies) {
            activity.finish();
        }
    }
}
